package com.gxa.ssm340.controller;

import com.gxa.ssm340.common.R;
import com.gxa.ssm340.entity.dto.AllExchangeListDTO;
import com.gxa.ssm340.entity.dto.CpnDTO;
import com.gxa.ssm340.entity.pojo.Coupon1;
import com.gxa.ssm340.entity.vo.AllExchangeVO;
import com.gxa.ssm340.exception.BizException;
import com.gxa.ssm340.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(value = "用户积分兑换明细",tags = "用户积分兑换明细接口")
@RestController
public class AllExchangeController {
    @Autowired
    private CouponService couponService;

    @ApiOperation(value = "查询所有用户积分兑换明细方法")
    @GetMapping("/admin/allPointsExchangeList")
    public R allPointsExchangeList(@Param("dto") AllExchangeListDTO dto) throws BizException, ParseException {
        // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // String format = simpleDateFormat.format(dto.getFrom());
        // String format1 = simpleDateFormat.format(dto.getTo());
        // Date parse = simpleDateFormat.parse(format);
        // Date parse1 = simpleDateFormat.parse(format1);
        // dto.setFrom(parse);
        // dto.setTo(parse1);
        List<AllExchangeVO> allExchangeVOS = couponService.AllExchangeList(dto);
        return R.success(allExchangeVOS);
    }

    @ApiOperation(value = "查询所有用户积分兑换明细导出方法")
    @GetMapping("/user/exportCpnUsedList1")
    public void exportCpnUsedList(HttpServletResponse response) throws BizException, ParseException, IOException {
        // 创建一个工作簿对象
        Workbook workbook = new XSSFWorkbook();
        AllExchangeListDTO allExchangeListDTO = new AllExchangeListDTO();
        allExchangeListDTO.setPage(1);
        List<AllExchangeVO> allExchangeVOS = couponService.AllExchangeList(allExchangeListDTO);
        // 创建一个工作表对象
        Sheet sheet = workbook.createSheet("Data");

        // 创建行和单元格，并将数据写入单元格中
        int rowIndex = 0;
        int cellIndex = 0;
        Row row0 = sheet.createRow(rowIndex++);
        row0.createCell(cellIndex++).setCellValue("编号");
        row0.createCell(cellIndex++).setCellValue("姓名");
        row0.createCell(cellIndex++).setCellValue("手机号");
        row0.createCell(cellIndex++).setCellValue("兑换礼品");
        row0.createCell(cellIndex++).setCellValue("兑换积分");
        row0.createCell(cellIndex++).setCellValue("兑换日期");

        for (AllExchangeVO item : allExchangeVOS) {
            Row row = sheet.createRow(rowIndex++);
            cellIndex = 0;
            row.createCell(cellIndex++).setCellValue(item.getPointsId());
            row.createCell(cellIndex++).setCellValue(item.getName());
            row.createCell(cellIndex++).setCellValue(item.getPhone());
            row.createCell(cellIndex++).setCellValue(item.getBrandName());
            row.createCell(cellIndex++).setCellValue(item.getPoints());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (item.getEntryTime()!=null) {
                String format = simpleDateFormat.format(item.getEntryTime());
                row.createCell(cellIndex++).setCellValue(format);

            }
        }
// 自动调整所有列的单元格大小
        for (int i = 0; i < allExchangeVOS.get(0).getClass().getDeclaredFields().length; i++) {
            sheet.autoSizeColumn(i);
        }
        // 设置响应头信息，告诉浏览器响应的是一个Excel文件
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;");

        // 将Excel文件写入响应输出流中，返回给前端
        workbook.write(response.getOutputStream());

    }
}
